import { Material } from '@feng3d/core';
import { FS } from '@feng3d/filesystem';
import { mtlConverter } from '../converters/MTLConverter';
import { mtlParser } from '../parsers/MTLParser';

/**
 * OBJ模型MTL材质加载器
 */
export class MTLLoader
{
    /**
     * 加载MTL材质
     * @param path MTL材质文件路径
     * @param completed 加载完成回调
     */
    async load(path: string, completed?: (err: Error, materials: { [name: string]: Material; }) => void)
    {
        const content = await FS.fs.readString(path).then(null, (err) =>
        {
            completed(err, null);
        });

        if (!content)
        {
            return;
        }
        const mtlData = mtlParser.parser(content);
        mtlConverter.convert(mtlData, completed);
    }
}

/**
 * OBJ模型MTL材质加载器
 */
export const mtlLoader = new MTLLoader();
